package com.itheima.goods.controller;


import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.Page;
import com.itheima.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result<List<Brand>> findList() {
        List<Brand> brandList = brandService.findList();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("/{id}")
    public Result Brand(@PathVariable("id") int id) {

        Brand byId = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", byId);

    }

    @PostMapping
    public Result Brand(@RequestBody Brand brand) {

        brandService.insertBrand(brand);

        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody Brand brand) {

        brandService.update(brand);

        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {

        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestParam Map search) {

        List<Brand> list = brandService.list(search);
        return new Result<>(true, StatusCode.OK, "模糊查询成功", list);
    }

    /*  @GetMapping("/search/{page}/{size}")
      public Result findPage(@PathVariable("page") int page,@PathVariable("size") int size){
          Page<Brand> pageInfo = brandService.findPage(page, size);
          PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
          return new Result(true,StatusCode.OK,"查询成功",pageResult);
      }
  */
    @GetMapping("/search/{page}/{size}")
    public Result<List<Brand>> findPage(@RequestParam Map stringObjectMap, @PathVariable Integer page, @PathVariable Integer size) {
        Page<Brand> page1 = brandService.findPages(stringObjectMap, page, size);
        PageResult pageResult = new PageResult(page1.getTotal(), page1.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);

    }


}
